import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

//1.引入Element框架相关内容
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//2.引入EL图标相关内容 https://element-plus.org/zh-CN/component/icon.html#注册所有图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import dataV from '@jiaminghi/data-view'

import naive from 'naive-ui'

const BASE_URL =  'http://localhost:8080';//'https://61.139.65.135:36897'
window.BASE_URL = BASE_URL;

//保存全局主题配置
window.GlobeTheme = localStorage.getItem('GlobeTheme')=="null" ? null : localStorage.getItem('GlobeTheme');

//3.createApp(App)创建一个vue实例
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.config.globalProperties.BASE_URL = BASE_URL;

//4.app.use(ElementPlus)添加EL .use(store)添加全局存储vuex .use(router)添加路由router
//mount挂载,将前面.use的内容放到/public/index.html页面中
app.use(ElementPlus,{ locale: zhCn }).use(naive).use(dataV).use(store).use(router).mount('#app')

window.getUser = ()=> {
    return localStorage.user?JSON.parse(localStorage.user):null;
}

window.setUser = (user) => {
    localStorage.setItem('user',JSON.stringify(user));
}


// 解决页面在加载表格之后修改窗口大小引起的报错
const debounce = (fn, delay) => {
    let timer
    return (...args) => {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            fn(...args)
        }, delay)
    }
}

const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver{
    constructor(callback) {
        callback = debounce(callback, 200);
        super(callback);
    }
}
